package com.cqh.recruit.service.impl;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqh.recruit.mapper.ActivityCheckMapper;
import com.cqh.recruit.mapper.ActivityMapper;
import com.cqh.recruit.pojo.dto.LActivityCheckQueryDto;
import com.cqh.recruit.pojo.entity.Activity;
import com.cqh.recruit.pojo.entity.ActivityCheck;
import com.cqh.recruit.pojo.vo.LActivityCheckListVo;
import com.cqh.recruit.result.ErrorCode;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IActivityCheckService;
import com.cqh.recruit.service.IActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:
 * @Author: DengHao
 * @Date: 2025/10/18 18:54
 */
@Service
public class ActivityCheckServiceImpl extends ServiceImpl<ActivityCheckMapper, ActivityCheck> implements IActivityCheckService {
    @Autowired
    private ActivityCheckMapper activityCheckMapper;
    
    @Autowired
    private IActivityService activityService;
    
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public Page<LActivityCheckListVo> findLActivityCheckByQueryDto(LActivityCheckQueryDto lActivityCheckQueryDto) {

        Page page = new Page(lActivityCheckQueryDto.getPage(), lActivityCheckQueryDto.getLimit());
        return getBaseMapper().findLActivityCheckByQueryDto(page, lActivityCheckQueryDto);
    }

    @Override
    public LActivityCheckListVo findActivityCheckById(Long id) {
        return activityCheckMapper.findActivityCheckById(id);
    }
    
    @Override
    @Transactional
    public Result checkAndUpdateActivityCapacity(ActivityCheck activityCheck) {
        try {
            // 1. 获取活动ID
            Long activityId = activityCheck.getActivityId();
            System.out.println("检查活动容量 - ActivityCheck ID: " + activityCheck.getId());
            System.out.println("检查活动容量 - Activity ID: " + activityId);
            
            // 如果activityId为空，尝试从数据库中获取
            if (activityId == null) {
                ActivityCheck existingCheck = this.getById(activityCheck.getId());
                if (existingCheck != null) {
                    activityId = existingCheck.getActivityId();
                    System.out.println("从数据库获取到的 Activity ID: " + activityId);
                }
            }
            
            if (activityId == null) {
                return Result.buildFail(ErrorCode.ACTIVITY_INFO_INCOMPLETE, "活动ID不能为空");
            }
            
            // 2. 获取活动信息
            Activity activity = activityService.getById(activityId);
            System.out.println("查询到的活动信息: " + (activity != null ? "存在" : "不存在"));
            
            if (activity == null) {
                return Result.buildFail(ErrorCode.ACTIVITY_NOT_FOUND, "活动不存在，ID: " + activityId);
            }
            
            // 3. 检查活动是否还有名额
            Integer capacity = activity.getCapacity();
            Integer currentParticipants = activity.getCurrentParticipants();
            
            System.out.println("活动容量信息 - 最大容量: " + capacity + ", 当前人数: " + currentParticipants);
            
            if (capacity == null || currentParticipants == null) {
                return Result.buildFail(ErrorCode.ACTIVITY_INFO_INCOMPLETE, 
                    "活动人数信息不完整 - 容量: " + capacity + ", 当前人数: " + currentParticipants);
            }
            
            // 4. 判断是否还有剩余名额
            if (currentParticipants >= capacity) {
                System.out.println("活动人数已满 - 当前: " + currentParticipants + ", 容量: " + capacity);
                return Result.buildFail(ErrorCode.ACTIVITY_CAPACITY_FULL, 
                    "活动人数已满，无法通过审核 (当前: " + currentParticipants + "/" + capacity + ")");
            }
            
            // 5. 使用原子性更新活动当前参与人数
            System.out.println("尝试更新活动参与人数，活动ID: " + activityId);
            int updatedRows = activityMapper.incrementParticipants(activityId);
            System.out.println("更新结果 - 影响行数: " + updatedRows);
            
            if (updatedRows == 0) {
                return Result.buildFail(ErrorCode.UPDATE_PARTICIPANTS_FAILED, 
                    "活动人数已满或更新失败 (可能存在并发冲突)");
            }
            
            return Result.buildSuccess();
            
        } catch (Exception e) {
            throw new RuntimeException("检查活动容量失败: " + e.getMessage(), e);
        }
    }

}
